package com.ruoyi.web.controller.consume;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consume.domain.PlugApplyCon;
import com.ruoyi.consume.service.IPlugApplyConService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 穴盘领用Controller
 *
 * @author ruoyi
 * @date 2021-07-23
 */
@Api("穴盘领用")
@RestController
@RequestMapping("/consume/plugap")
public class PlugApplyConController extends BaseController
{
    @Autowired
    private IPlugApplyConService plugApplyService;

    /**
     * 查询穴盘领用列表
     */
    @ApiOperation("查询穴盘领用列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:plugap:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlugApplyCon plugApplyCon)
    {
        startPage();
        List<PlugApplyCon> list = plugApplyService.selectPlugApplyList(plugApplyCon);
        return getDataTable(list);
    }

    /**
     * 导出穴盘领用列表
     */
    @PreAuthorize("@ss.hasPermi('consume:plugap:export')")
    @Log(title = "穴盘领用", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PlugApplyCon plugApplyCon)
    {
        List<PlugApplyCon> list = plugApplyService.selectPlugApplyList(plugApplyCon);
        ExcelUtil<PlugApplyCon> util = new ExcelUtil<PlugApplyCon>(PlugApplyCon.class);
        return util.exportExcel(list, "穴盘领用数据");
    }

    /**
     * 获取穴盘领用详细信息
     */
    @PreAuthorize("@ss.hasPermi('consume:plugap:query')")
    @GetMapping(value = "/{plugApplyId}")
    public AjaxResult getInfo(@PathVariable("plugApplyId") Long plugApplyId)
    {
        return AjaxResult.success(plugApplyService.selectPlugApplyById(plugApplyId));
    }

    /**
     * 新增穴盘领用
     */
    @PreAuthorize("@ss.hasPermi('consume:plugap:add')")
    @Log(title = "穴盘领用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlugApplyCon plugApplyCon)
    {
        return toAjax(plugApplyService.insertPlugApply(plugApplyCon));
    }

    /**
     * 修改穴盘领用
     */
    @PreAuthorize("@ss.hasPermi('consume:plugap:edit')")
    @Log(title = "穴盘领用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlugApplyCon plugApplyCon)
    {
        return toAjax(plugApplyService.updatePlugApply(plugApplyCon));
    }

    /**
     * 删除穴盘领用
     */
    @PreAuthorize("@ss.hasPermi('consume:plugap:remove')")
    @Log(title = "穴盘领用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{plugApplyIds}")
    public AjaxResult remove(@PathVariable Long[] plugApplyIds)
    {
        return toAjax(plugApplyService.deletePlugApplyByIds(plugApplyIds));
    }

    /**
     * 更新库存量表
     */
    @ApiOperation("更新库存量表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:plugap:amount')")
    @Log(title = "穴盘领用", businessType = BusinessType.UPDATE)
    @PutMapping("/amount")
    public AjaxResult amount(@RequestBody PlugApplyCon plugApplyCon)
    {
        int row = plugApplyService.updatePlugAmount(plugApplyCon);
        if(row == 0){
            return error("库存不足");
        }
        else {
            return toAjax(row);
        }
    }

    /**
     * 更新领用状态
     */
    @ApiOperation("更新领用状态")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:plugap:consu')")
    @Log(title = "穴盘领用", businessType = BusinessType.UPDATE)
    @PutMapping("/consu/{plugApplyId}")
    public AjaxResult consume(@PathVariable("plugApplyId") Long plugApplyId)
    {
        return toAjax(plugApplyService.updateConsumeStatus(plugApplyId));
    }

    /**
     * 退回操作
     */
    @ApiOperation("退回操作")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:plugap:back')")
    @Log(title = "穴盘领用", businessType = BusinessType.UPDATE)
    @PutMapping("/back/{plugApplyId}")
    public AjaxResult back(@PathVariable("plugApplyId") Long plugApplyId)
    {
        return toAjax(plugApplyService.updateApprovalStatus(plugApplyId));
    }
}
